package tool;

import bean.ModelType;
import io.reactivex.Observable;
import io.reactivex.Scheduler;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import joinery.DataFrame;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;
import ui.model.BaseModel;
import ui.model.ElectiveModel;
import ui.model.LogModel;
import ui.model.TableModel;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static tool.TableProperty.*;

public class UIEventSender {
    private static UIEventSender instance;
    private UIEventSender()
    {}
    public static UIEventSender getInstance()
    {
        if (instance == null)
        {
            instance = new UIEventSender();
        }
        return instance;
    }
    private Map<ModelType, BaseModel> panel = new HashMap<>();
    public void Regeist(ModelType model, BaseModel component)
    {
        panel.put(model,component);
    }

    public void Log(String i)
    {
        Log(null,i);
    }
    public void Log(String tag,String... i)
    {
        if (panel.containsKey(ModelType.Log))
        {
            LogModel logPanel = (LogModel) panel.get(ModelType.Log);
            logPanel.Log(tag,i);

        }
    }
    public void TestUnLog()
    {
        if (panel.containsKey(ModelType.Log))
        {
            LogModel logPanel = (LogModel) panel.get(ModelType.Log);
//            logPanel.RemoveLog();
            Lg.info("remove");
        }
    }
    public void LoadAllData(String type)
    {
        if (panel.containsKey(ModelType.Table))
        {
//            Observable<ResultScanner> resultScannerObservable = DataProvider.catchAllFromType(type);
            final TableModel model = (TableModel) panel.get(ModelType.Table);
            Observable.just(DataProvider.loadAllFromType(type))
                    .observeOn(Schedulers.io())
                    .observeOn(Schedulers.computation())
                    .subscribe(new Consumer<DataFrame<String>>() {
                        @Override
                        public void accept(DataFrame<String> dataFrame) throws Exception {
//                            Lg.info("model.onLoaded(dataFrame);");
                            model.onLoaded(dataFrame);
                        }
                    });
//            DataFrame<String> dataFrame =

        }
    }
    public void NotifyDataLoad(String type)
    {
        if (panel.containsKey(ModelType.Table))
        {
            TableModel model = (TableModel) panel.get(ModelType.Table);
            model.notifyTypeUpdata(type);
        }
    }
    public void ChooseClassCall(List<String> row)
    {
        if (panel.containsKey(ModelType.Elective))
        {
            ElectiveModel model = (ElectiveModel) panel.get(ModelType.Elective);
            model.chooseFromFilter(row);
        }else
        {
//            Lg.info(row.toArray());
        }
    }
    public void ChooseSidCall(List<String> sidrow)
    {
        if (isContainer(ModelType.Elective))
        {
            ElectiveModel model = (ElectiveModel) panel.get(ModelType.Elective);
//            Lg.info(sidrow);
            model.chooseFromSid(sidrow.get(1));
        }
    }

    private boolean isContainer(ModelType modelType)
    {
        return panel.containsKey(modelType);
    }
}
